package org.jeecg.modules.controller;

import java.util.Arrays;
import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonCuckoo;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.common.util.TokenUtils;
import org.jeecg.modules.entity.CuUserDetails;
import org.jeecg.modules.entity.CuVipCard;
import org.jeecg.modules.payment.vo.PaymentResponse;
import org.jeecg.modules.service.ICuUserDetailsService;
import org.jeecg.modules.service.ICuVipCardService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.utils.GeneralToolsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

/**
 * @Description: 会员卡卷
 * @Author: jeecg-boot
 * @Date: 2024-07-17
 * @Version: V1.0
 */
@Tag(name = "会员卡卷")
@RestController
@RequestMapping("/cuckoo/cuVipCard")
@Slf4j
public class CuVipCardController extends JeecgController<CuVipCard, ICuVipCardService> {
    @Autowired
    private ICuVipCardService vipCardService;

    @Autowired
    private ICuUserDetailsService userDetailsService;

    /**
     * 分页列表查询
     *
     * @param vipCard
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "会员卡卷-分页列表查询")
    @Operation(summary = "会员卡卷-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<CuVipCard>> queryPageList(CuVipCard vipCard,
                                                  @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                  @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                  HttpServletRequest req) {
        QueryWrapper<CuVipCard> queryWrapper = QueryGenerator.initQueryWrapper(vipCard, req.getParameterMap());
        Page<CuVipCard> page = new Page<CuVipCard>(pageNo, pageSize);
        IPage<CuVipCard> pageList = vipCardService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * -列表查询
     *
     * @param req
     * @return
     */
    @Operation(summary = "会员卡卷-列表查询")
    @GetMapping(value = "/vipList")
    public Result<List<CuVipCard>> vipList(HttpServletRequest req) {
        List<CuVipCard> list = vipCardService.vipList();
        return Result.OK(list);
    }

    /**
     * 添加
     *
     * @param vipCard
     * @return
     */
    @AutoLog(value = "会员卡卷-添加")
    @Operation(summary = "会员卡卷-添加")
    @RequiresPermissions("cuckoo:cu_vip_card:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody CuVipCard vipCard) {
        vipCardService.save(vipCard);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param vipCard
     * @return
     */
    @AutoLog(value = "会员卡卷-编辑")
    @Operation(summary = "会员卡卷-编辑")
    @RequiresPermissions("cuckoo:cu_vip_card:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody CuVipCard vipCard) {
        vipCardService.updateById(vipCard);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "会员卡卷-通过id删除")
    @Operation(summary = "会员卡卷-通过id删除")
    @RequiresPermissions("cuckoo:cu_vip_card:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        vipCardService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "会员卡卷-批量删除")
    @Operation(summary = "会员卡卷-批量删除")
    @RequiresPermissions("cuckoo:cu_vip_card:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.vipCardService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "会员卡卷-通过id查询")
    @Operation(summary = "会员卡卷-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<CuVipCard> queryById(@RequestParam(name = "id", required = true) String id) {
        CuVipCard vipCard = vipCardService.getById(id);
        if (vipCard == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(vipCard);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param vipCard
     */
    @RequiresPermissions("cuckoo:cu_vip_card:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, CuVipCard vipCard) {
        return super.exportXls(request, vipCard, CuVipCard.class, "会员卡卷");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequiresPermissions("cuckoo:cu_vip_card:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, CuVipCard.class);
    }

    /**
     * 购买会员卡卷
     */
    @AutoLog(value = "会员卡卷-购买会员卡卷")
    @Operation(summary = "会员卡卷-购买会员卡卷")
    @RequiresPermissions("cuckoo:cu_vip_card:buyVipCard")
    @PostMapping(value = "/buyVipCard")
    public Result<String> buyVipCard(@RequestBody CuVipCard vipCard, HttpServletRequest request) {
        // 获取ip地址
        String clientIp = GeneralToolsUtil.getClientIpAddr(request);

        //获取当前用户的token 下单支付
        String userName = JwtUtil.getUserNameByToken(request);
        CuUserDetails userDetails = userDetailsService.getByUsername(userName);
        if (ObjectUtil.isEmpty(userDetails)) {
            return Result.error("用户不存在");
        }
        // 获取会员卡卷
        vipCard = vipCardService.getById(vipCard.getId());
        if (ObjectUtil.isEmpty(vipCard)) {
            return Result.error("会员卡卷不存在");
        }
        if (vipCard.getStart().equals(CommonCuckoo.STATUS_0)) {
            return Result.error("会员卡卷已下架");
        }
        PaymentResponse response = vipCardService.buyVipCard(vipCard, userDetails, clientIp);
        if (ObjectUtil.isEmpty(response) || response.getCode() != 1) {
            return Result.error("购买失败");
        }

        return Result.OK("请支付，完成后重新启动重新即可", response.getPayurl());
    }

}
